package com.example.lenovo.demo.presenter.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.lenovo.demo.R;
import com.example.lenovo.demo.model.databean.CityBean;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Lenovo on 2017/1/8.
 */

public class City_Destination_RvAdapter extends RecyclerView.Adapter<City_Destination_RvAdapter.ViewHolder> implements View.OnClickListener{
    private List<CityBean.DataBean.SectionsBean.ModelsBean> data;
    private Context context;
    private OnItemClickListener listener1;
    private RecyclerView rv;

    //自定义接口，实现点击的调用
    public interface OnItemClickListener{
        void onItemClick(int index);
    }
    //绑定对应的RecyclerView
    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.rv=recyclerView;
    }

    //设置项的点击事件
    public void setOnItemClick(OnItemClickListener listener1) {
        this.listener1 = listener1;
    }
    public City_Destination_RvAdapter(List<CityBean.DataBean.SectionsBean.ModelsBean> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public City_Destination_RvAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView=LayoutInflater.from(context).inflate(R.layout.home_gv_item,parent,false);
        itemView.setOnClickListener(this);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(City_Destination_RvAdapter.ViewHolder holder, int position) {
        holder.name.setText(data.get(position).getName());
        holder.subName.setText(data.get(position).getTitle());
        Picasso.with(context).load(data.get(position).getPhoto_url()).into(holder.imageView);

    }

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }

    @Override
    public void onClick(View view) {
        //验证是否设置了点击事件
        if(listener1!=null)
        {
            //根据当前itemView获取对应的索引
            int index=rv.getChildAdapterPosition(view);
            listener1.onItemClick(index);
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView name,subName;
        public ViewHolder(View itemView) {
            super(itemView);
            imageView= (ImageView) itemView.findViewById(R.id.home_gv_img);
            name= (TextView) itemView.findViewById(R.id.home_gv_name);
            subName= (TextView) itemView.findViewById(R.id.home_gv_subname);
        }
    }
}
